<?php

namespace App\Http\Controllers;

use App\Models\Business;
use App\Models\Record;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Resources\Record as RecordResource;

class PortraitController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
//        if ($request->department_id && $request->date){
//            abort(404,'信息错误');
//        }
        $departmentId = $request->department_id;
        $portratiDate = $request->date;


        $data_cycle = data_cycles_from_date($request->date);

        if ($data_cycle['month']->greaterThan(Carbon::now())){
            abort(404, '日期错误');
        }

        $monthIds = Business::getBusinessIdByDataCycle('月');
        $quarterIds = Business::getBusinessIdByDataCycle('季度');
        $halfYearIds = Business::getBusinessIdByDataCycle('半年');
        $yearIds = Business::getBusinessIdByDataCycle('年');

        $monthRecords = Record::where('department_id','=',$departmentId)->whereIn('business_id',$monthIds)->whereDate('record_date',$data_cycle['month'])->get();
        $quarterRecords = Record::where('department_id','=',$departmentId)->whereIn('business_id',$quarterIds)->whereDate('record_date',$data_cycle['quarter'])->get();
        $halfYearRecords = Record::where('department_id','=',$departmentId)->whereIn('business_id',$halfYearIds)->whereDate('record_date',$data_cycle['half_year'])->get();
        $yearRecords = Record::where('department_id','=',$departmentId)->whereIn('business_id',$yearIds)->whereDate('record_date',$data_cycle['year'])->get();
        $records = $monthRecords->merge($quarterRecords)->merge($halfYearRecords)->merge($yearRecords);

        return RecordResource::collection($records);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function overall(Request $request)
    {

    }
}
